import unittest
from alg3 import Solution 
from gen3 import generate_multiple_test_cases

class TestShoppingOffers(unittest.TestCase):
    def test_example_case(self):
        """测试示例用例"""
        solution = Solution()
        
        # LeetCode上的示例
        price = [2, 5]
        special = [[3, 0, 5], [1, 2, 10]]
        needs = [3, 2]
        self.assertEqual(solution.shoppingOffers(price, special, needs), 14)
        
        # 另一个示例
        price = [2, 3, 4]
        special = [[1, 1, 0, 4], [2, 2, 1, 9]]
        needs = [1, 2, 1]
        self.assertEqual(solution.shoppingOffers(price, special, needs), 11)
    
    def test_single_item_no_specials(self):
        """测试只有一种物品且没有特价套餐的情况"""
        solution = Solution()
        price = [5]
        special = []
        needs = [3]
        self.assertEqual(solution.shoppingOffers(price, special, needs), 15)
    
    def test_multiple_items_no_specials(self):
        """测试多种物品但没有特价套餐的情况"""
        solution = Solution()
        price = [3, 4, 2]
        special = []
        needs = [2, 1, 3]
        self.assertEqual(solution.shoppingOffers(price, special, needs), 3*2 + 4*1 + 2*3)
    
    def test_special_cheaper_than_alone(self):
        """测试使用特价套餐更便宜的情况"""
        solution = Solution()
        price = [5, 7]
        special = [[1, 1, 10], [2, 0, 8], [0, 2, 12]]
        needs = [2, 2]
        
        # 单独购买: 2*5 + 2*7 = 24
        # 使用套餐: 套餐1(1,1,10)用2次，10*2 = 20
        self.assertEqual(solution.shoppingOffers(price, special, needs), 20)
    
    def test_random_generated_cases(self):
        """测试随机生成的用例"""
        solution = Solution()
        test_cases = generate_multiple_test_cases(5)  # 生成5个随机测试用例
        
        for price, special, needs in test_cases:
            # 确保代码能正常运行，不抛出异常
            try:
                solution.shoppingOffers(price, special, needs)
            except Exception as e:
                self.fail(f"测试用例失败: price={price}, special={special}, needs={needs}. 异常: {e}")

if __name__ == '__main__':
    unittest.main()    